﻿using System;
using System.Web;
using System.IO;
using Newtonsoft.Json;
using XWG.JWBST.JWBSTEntity;
using XWG.Framework.AuthUtility;
using XWG.JWBST.ProcessInterfaces;
using Newtonsoft.Json.Linq;


namespace XWG.JWBST.OneButtonAlarmProcess
{
    /// <summary>
    /// 一键报警
    /// </summary>
    public class Process : IProcess
    {
        /// <summary>
        /// 一键报警
        /// </summary>
        /// <param name="reader">接受的数据</param>
        /// <param name="context">请求信息</param>
        /// <returns>操作结果</returns>
        public string ProcessData(StreamReader reader, HttpContext context)
        {
            // 读取提交数据
            string receivemes = reader.ReadToEnd();

            // UrlDecode解码
            receivemes = HttpUtility.UrlDecode(receivemes);

            // 去除content=无用数据 提交的时候不要此key

            //receivemes = receivemes.Replace("content=", "");

            // 反序列化json对象
            JObject oka = (JObject)JsonConvert.DeserializeObject(receivemes);
            TbBstOnekeyAlarm oneKeyAlarm = new TbBstOnekeyAlarm();
            oneKeyAlarm.OnekeyAlarmPkid = Guid.NewGuid().ToString();
            //oneKeyAlarm.UserId = oka["userID"].ToString().Replace("\"", string.Empty);
            //oneKeyAlarm.GpsX = oka["GPS_JD"].ToString().Replace("\"", string.Empty);
            //oneKeyAlarm.GpsY = oka["GPS_WD"].ToString().Replace("\"", string.Empty);
            oneKeyAlarm.PhoneNo = oka["phone"].ToString().Replace("\"", string.Empty);
            oneKeyAlarm.ProcessResult = "未处理";
            oneKeyAlarm.Deltag = 0;
            oneKeyAlarm.AlarmDate = DateTime.Now;

            //记录请求信息
            JWBSTEntity.TbBstVisits visit = new JWBSTEntity.TbBstVisits();
            visit.Id = Guid.NewGuid().ToString();
            visit.Phone = oneKeyAlarm.PhoneNo;
            visit.Time = DateTime.Now;
            visit.Modulename = "OneButtonAlarm";
            visit.Optionname = "一键报警";
            visit.Other = oka.ToString();
            JWBSTBLL.TbBstVisitsBLL.Insert(visit, "", false);

            // 执行数据库插入操作
            ResultModel resultModel = XWG.JWBST.JWBSTBLL.TbBstOnekeyAlarmBLL.Insert(oneKeyAlarm, string.Empty, false);


            // 返回结果
            return resultModel.Message;
        }
    }
}
